﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace GalaxyDefenderChaos
{
    public class SpaceShip : CModel
    {
        BillBoard _HpFillBar;

        private int _teamID;

        public int TeamID
        {
            get { return _teamID; }
            set { _teamID = value; }
        }

        private int _Hp;
        private Microsoft.Xna.Framework.Graphics.SpriteFont _SpriteFont;

        bool _IsDrawHPBar = false;

        public bool IsDrawHPBar
        {
            get { return _IsDrawHPBar; }
            set { _IsDrawHPBar = value; }
        }

        public int Hp
        {
            get { return _Hp; }
            set { _Hp = value; }
        }

        private double _timeReloadRocket;

        public double TimeReloadRocket
        {
            get { return _timeReloadRocket; }
            set { _timeReloadRocket = value; }
        }

        private double _delaytimeReloadOffset;

        public double DelaytimeReloadOffset
        {
            get { return _delaytimeReloadOffset; }
            set { _delaytimeReloadOffset = value; }
        }

        public SpaceShip(Model model, Vector3 Position, Vector3 Rotation,
             Vector3 Scale, GraphicsDevice graphicsDevice, int teamID, ContentManager content, Camera camera) : base( model,  Position,  Rotation,
              Scale,  graphicsDevice)
        {
            TeamID = teamID;
            Hp = 100;

            _HpFillBar = new BillBoard(content, "Bar/RedFillBar", new Vector2(1000, 200), Position, Vector3.Up, graphicsDevice, camera);

            _SpriteFont = content.Load<SpriteFont>("hudFont");
            DelaytimeReloadOffset = 6000;
            Speed = 5;
        }

        public CModel FindNearObject(List<CModel> listTarget)
        {
            CModel target = null;
            double length = 1000000;
            for (int i = 0; i < listTarget.Count; i++)
            {
                double tempLenghth = Math.Sqrt(
                    Math.Pow(this.Position.X - listTarget[i].Position.X, 2) +
                    Math.Pow(this.Position.Y - listTarget[i].Position.Y, 2) +
                    Math.Pow(this.Position.Z - listTarget[i].Position.Z, 2));
                if (tempLenghth < length)
                {
                    length = tempLenghth;
                    target = listTarget[i];
                }
            }
            return target;
        }

        public override void Draw(Microsoft.Xna.Framework.Graphics.GraphicsDevice graphicsDevice, Camera camera, Microsoft.Xna.Framework.GameTime gameTime)
        {
            camera.Update();
            if (IsDrawHPBar) {
                _HpFillBar.Position = Position;

                Vector2 newSizeHPFill = _HpFillBar.Size;
                newSizeHPFill.X = 1000 * Hp / 100;
                _HpFillBar.Size = newSizeHPFill;

                _HpFillBar.Update(gameTime, new TouchCollection());
                _HpFillBar.Draw(graphicsDevice, camera, gameTime);
            }
            //DrawHpInfor(graphicsDevice);
            base.Draw(graphicsDevice, camera, gameTime);
        }

        public override void Update(GameTime gameTime, Microsoft.Xna.Framework.Input.Touch.TouchCollection touchCollection)
        {
            if (TimeReloadRocket <= DelaytimeReloadOffset)
                TimeReloadRocket += gameTime.ElapsedGameTime.TotalMilliseconds;
            base.Update(gameTime, touchCollection);
        }

        public void RunOverRocket(GameTime gameTime, CModel rocketTarget)
        {
            float _speed = 5;
            if (rocketTarget != null)
            {
                double angle = Math.Atan2(rocketTarget.Position.X - this.Position.X,
                        rocketTarget.Position.Z - this.Position.Z);

                this.Rotation = new Vector3(0, (float)angle, 0);

                Matrix rotation = Matrix.CreateFromYawPitchRoll(this.Rotation.Y, this.Rotation.X, this.Rotation.Z);

                this.Position += Vector3.Transform(Vector3.Forward, rotation) * (float)gameTime.ElapsedGameTime.TotalMilliseconds * _speed;
            }
        }

        public override void ChaseObject(Microsoft.Xna.Framework.GameTime gameTime, CModel destination)
        {
            double tempLenghth = Math.Sqrt(
                        Math.Pow(this.Position.X - destination.Position.X, 2) +
                        Math.Pow(this.Position.Y - destination.Position.Y, 2) +
                        Math.Pow(this.Position.Z - destination.Position.Z, 2));
            if(tempLenghth > 10000)
            {
                Vector3 rotaterocket = destination.Position - this.Position;
            
                rotaterocket.Normalize();
                this.Position += rotaterocket * (float)gameTime.ElapsedGameTime.TotalMilliseconds * Speed;

            }
            double angle = Math.Atan2(this.Position.X - destination.Position.X,
                    this.Position.Z - destination.Position.Z);

            this.Rotation = new Vector3(0, (float)angle, 0);
            //base.ChaseObject(gameTime, destination);
        }
    }
}
